home *** CD-ROM | disk | FTP | other *** search
- Path: castle.nando.net!news
- From: actuary@nando.net (Bill McCarthy)
- Newsgroups: comp.lang.c
- Subject: Re: Help with simple code
- Date: 14 Jan 1996 20:02:13 GMT
- Organization: News & Observer Public Access
- Message-ID: <4dbng5$e9o@castle.nando.net>
- References: <4dbak5$oij@ionews.io.org>
- Reply-To: actuary@nando.net (Bill McCarthy)
- NNTP-Posting-Host: grail801.nando.net
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4dbak5$oij@ionews.io.org>, jgordon@io.org (John Gordon MacPherson) writes:
- >Can anyone tell me what's wrong with this piece of code? I lifted it
- >straight from a textbook.
- >
- >Here's the code:
- >
- >/* Calculating compound interest */
- >#include <stdio.h>
- >#include <math.h>
- >
- >main()
- >{
- > int year;
- > double amount, principal = 1000, rate = 0.5;
- >
- > printf("%4s%21s\n", "Year", "Amount on deposit");
- >
- > for (year = 1; year <= 10; year++) {
- > amount = principal * pow(1.0 + rate, year);
- > printf("%4d%21.2f\n", year, amount);
- > }
- >
- > return 0;
- >}
- >______________________________________________________________
- >
- >and here's the error:
- >
- >In function `main':
- >undefined reference to `pow'
- >
- >I don't understand. `pow' is a function, not a variable?
- >Can anyone tell me what's wrong?
-
- If that is a compiler error, you need a new compiler. If it was
- a link error, you probably have not set up your environment
- properly. Your book's code is OK but not efficient. There is
- no need for using pow().
-
- You could initialize <amount = principal;> and replace the first
- line of the 'for' loop with <amount *= 1 + rate;>
-
- Bill McCarthy
- actuary@nando.net
- Wendell, NC USA
-
-